/**
    Skill.08 >>> 文件存储class
 */
const Store = require('electron-store')
const uuidv4 = require('uuid/v4')
const path = require('path')

class DataFileStore extends Store {
  constructor(settings, key) {
    super(settings)
    this.data = this.get(key) || []
    this.key = key
  }

  saveData() {
    this.set(this.key, this.data)
    return this
  }

  getData() {
    return this.get(this.key) || []
  }

  addData(data) {
    const dataProps = data.map(x => {
      return {
        id: uuidv4(),
        path: x,
        fileName: path.basename(x)
      }
    }).filter(x => {
      // 过滤重复数据
      const currentData = this.getData().map(y => y.path)
      // return currentData.indexOf(x.path) < 0
      return !currentData.includes(x.path)
    })
    // 数据重新拼接
    this.data = [...this.data, ...dataProps]
    return this.saveData()
  }
}

module.exports = DataFileStore